﻿package view.components.contacts { 
	
	import flash.display.MovieClip;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import org.papervision3d.events.FileLoadEvent;

	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.materials.VideoStreamMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.events.InteractiveScene3DEvent;
	
	import flash.events.Event;
	import gs.TweenLite;
	import gs.easing.*;
	import view.components.IMainComponent;
	
	import org.casalib.util.NavigateUtil;
	
	public class ContactPage extends BasicView implements IMainComponent {
		
		private var _contactsTwoSidePlane:ContactsTwoSidedPlane;
		
		public function ContactPage($xmlData:XMLList, viewportWidth:Number =  550, viewportHeight:Number = 550,	
								scaleToStage:Boolean=false, interactive:Boolean=true, cameraType:String="CAMERA3D")
		{
			super(viewportWidth, viewportHeight, scaleToStage, interactive, cameraType);
			viewport.useHandCursor = true;
			
			_contactsTwoSidePlane = new ContactsTwoSidedPlane($xmlData);
			_contactsTwoSidePlane.scale = 1.6;	
			_contactsTwoSidePlane.alpha = 0;
			camera.target = _contactsTwoSidePlane;
			
			createEventsMailMaterial(_contactsTwoSidePlane.linkMailArea, "katnikova@gmail.com");
			createEventsAboutMaterial(_contactsTwoSidePlane.aboutLinkArea);
			createEventsAboutPlane(_contactsTwoSidePlane.aboutPlane);
			
			
			scene.addChild(_contactsTwoSidePlane);
			renderer.renderScene(scene, camera, viewport);
			this.startRendering();
			
			TweenLite.to(_contactsTwoSidePlane, 3,{ ease:Cubic, scaleY:2.3, scaleX:2.3, alpha:1 } );
		}
		
		/**
		 * Simple create out over handlers MAIL
		 * @param	$plane
		 */
		private function createEventsMailMaterial($plane:Plane, $link):void 
		{
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, clickMaterialHandler, false, 0, false);
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, overMaterialHandler, false, 0, false);
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, outMaterialHandler, false, 0, true);
			
			function clickMaterialHandler(e:InteractiveScene3DEvent):void {
				NavigateUtil.openUrl($link, NavigateUtil.WINDOW_BLANK);
			}
			
			function outMaterialHandler(e:InteractiveScene3DEvent):void {
				viewport.buttonMode = false;
			}
			
			function overMaterialHandler(e:InteractiveScene3DEvent):void {
				viewport.buttonMode = true;
			}
		}
		
		/**
		 * Simple create out over handlers Title
		 * @param	$plane
		 */
		private function createEventsAboutMaterial($plane:Plane):void 
		{
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, clickMaterialHandler, false, 0, false);
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, overMaterialHandler, false, 0, false);
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, outMaterialHandler, false, 0, true);
			
			function clickMaterialHandler(e:InteractiveScene3DEvent):void {
					TweenLite.to(_contactsTwoSidePlane, 1, { ease:Elastic, rotationY: -180 } );
					TweenLite.to(_contactsTwoSidePlane.titleMovie, 1, { alpha:1, tint:0xffffff, ease:Cubic } );
			}
			
			function outMaterialHandler(e:InteractiveScene3DEvent = null):void {
				viewport.buttonMode = false;
				TweenLite.to(_contactsTwoSidePlane.titleMovie, 1, { alpha:1, tint:0xffffff, ease:Cubic } );
			}
			
			function overMaterialHandler(e:InteractiveScene3DEvent):void {
				viewport.buttonMode = true;
				TweenLite.to(_contactsTwoSidePlane.titleMovie, 1, { alpha:.5, tint:0xff3333, ease:Cubic } );
			}
		}
		
		/**
		 * Simple create out all handlers About
		 * @param	$plane
		 */
		private function createEventsAboutPlane($plane:Plane):void 
		{
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, clickMaterialHandler, false, 0, false);
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_OVER, overMaterialHandler, false, 0, false);
			$plane.addEventListener(InteractiveScene3DEvent.OBJECT_OUT, outMaterialHandler, false, 0, true);
			
			function clickMaterialHandler(e:InteractiveScene3DEvent):void {
					TweenLite.to(_contactsTwoSidePlane, 1, { ease:Elastic, rotationY: 0 } );
					viewport.buttonMode = false;
			}
			
			function outMaterialHandler(e:InteractiveScene3DEvent):void {
				viewport.buttonMode = false;
			}
			
			function overMaterialHandler(e:InteractiveScene3DEvent):void {
				viewport.buttonMode = true;
			}
		}
		
		protected override function onRenderTick(event:Event = null):void {
			var rotY: Number = (mouseY-(stage.stageHeight/2))/(stage.height/2)*(700);
			var rotX: Number = (mouseX-(stage.stageWidth/4))/(stage.width/2)*(-500);
			camera.x= camera.x+(rotX-camera.x)/15;
			camera.y= camera.y+(rotY-camera.y)/15;
			renderer.renderScene(scene, camera, viewport);
		}
		
		private function handleFileLoadError(e:FileLoadEvent):void {
			trace('handleFileLoadError'+e);
		}
		
		public function setPosition(w:Number, h:Number):void {
			this.x = w / 2 - (w / 2);
			this.y = h / 2 - this.height/2;
		}
	}
}